package top.dancun.eascourse.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.dancun.common.entity.RoleEntity;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.R;
import top.dancun.eascourse.entity.ClassScheduleEntity;
import top.dancun.eascourse.entity.CourseEntity;
import top.dancun.eascourse.entity.StudentEntity;
import top.dancun.eascourse.exception.CourseConflictsException;
import top.dancun.eascourse.feign.service.SysFeignService;
import top.dancun.eascourse.service.ClassScheduleService;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : dancun
 * @date 2022/10/22 18:21
 */
@RestController
@RequestMapping("/curriculum")
public class ClassScheduleController extends AbstractController{

    @Autowired
    private ClassScheduleService service;

    @Autowired
    private SysFeignService sysFeignService;

    /**
     * 获取我的课表
     * @param params
     * @return
     */
    @RequestMapping("/getusercurriculum")
    public R getUserCurriculum(@RequestBody  Map<String,String> params){
        String userId = getUserId();
        params.put("userId",userId);
        return R.ok().put("data",service.getCurriculum(params));
    }

    /**
     * 获取课表
     * @param params
     * @return
     */
    @RequestMapping("/getcurriculum")
    @RequiresPermissions("eascourse:curriculum:list")
    public R getCurriculum(@RequestBody  Map<String,String> params){
        return R.ok().put("data",service.getCurriculum(params));
    }

    /**
     * 获取我的所有课程
     * @return
     */
    @RequestMapping("/getmycourse")
    public R getMyCourse(@RequestBody Map<String,Object> params){
        String uid = getUserId();
        List<RoleEntity> roleEntityList = sysFeignService.ListMyRole();
        for(RoleEntity role :roleEntityList){
            if(role.getRoleName().equals("超级管理员")) {//管理员
                return  getAllCourse(params);
            }
        }


        PageUtils pageUtils=  service.getMyCourse(getUserId(),params);
        return  R.ok().put("data",pageUtils);

    }
    /**
     * 获取所有课程
     * @return
     */
    @RequestMapping("/getallcourse")
    public R getAllCourse(@RequestBody Map<String,Object> params){


        PageUtils pageUtils =  service.getAllCourse(params);
        return  R.ok().put("data",pageUtils);

    }


    /**
     * 获取教师所教学科的所有学生
     * @return
     */
    @RequestMapping("/getstudent")
    public R getStudent(@RequestBody Map<String, String> params){
        String uid = getUserId();

        PageUtils studentEntityList =  service.getStudent(params,getUserId());
        return  R.ok().put("page",studentEntityList);

    }

    @RequestMapping("/save")
    @RequiresPermissions("eascourse:curriculum:save")
    public R save(@RequestBody ClassScheduleEntity classScheduleEntity) throws CourseConflictsException {


        service.saveClassSchedule(classScheduleEntity);
        return  R.ok();

    }

    @RequestMapping("/update")
    @RequiresPermissions("eascourse:curriculum:update")
    public R update(@RequestBody ClassScheduleEntity classScheduleEntity){
       service.updateById(classScheduleEntity);
        return  R.ok();

    }
    @RequestMapping("/delete")
    @RequiresPermissions("eascourse:curriculum:delete")
    public R delete(@RequestBody Integer id){
        service.removeById(id);
        return  R.ok();

    }

    @RequestMapping("/clear")
    @RequiresPermissions("eascourse:curriculum:delete")
    public R clear(@RequestBody List<Integer> ids){
        service.clear(ids);
        return  R.ok();

    }
}
